﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PH.Model.Systems;
namespace PH.Data.Systems
{
    public class NoticeCommand
    {
        public static NoticeModel GetModel(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Notices.FirstOrDefault(p => p.ID == id);
                    if (d != null)
                    {
                        var ret = Convertor.ConvertTo<NoticeModel, Notice>(d);
                        return ret;
                    }
                }
            }
            return null;
        }

        public static NoticeCollection Gets(NoticeQuery sea)
        {
            var ret = new NoticeCollection();
            using (DBCentre db = new DBCentre())
            {
                var n = 0;
                IQueryable<Notice> q = db.DataContext.Notices;
                if (sea != null)
                {
                    if (sea.Expired != null)
                    {
                        if (sea.Expired.Value)
                            q = q.Where(p => p.Expired.CompareTo(DateTime.Now) <= 0);
                        else
                            q = q.Where(p => p.Expired.CompareTo(DateTime.Now) > 0);
                    }
                }
                
                q = q.OrderByDescending(p => p.ID);
                if (sea != null && sea.PageSize > 0)
                {
                    n = q.Count();
                    q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                }

                var lst = q.ToList();
                ret = Convertor.ConvertCollection<NoticeModel, Notice, NoticeCollection>(lst);
                ret.TotalRecordsCount = n == 0 ? lst.Count : n;
            }
            return ret;
        }

        public static void SaveCheck(NoticeModel model, DBCentre db)
        {
        }

        public static void CreateNew(NoticeModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    SaveCheck(model, db);
                    var d = Convertor.ConvertFrom<Notice, NoticeModel>(model);
                    d.CreatedOn = DateTime.Now;
                    db.DataContext.Notices.Add(d);
                    db.DataContext.SaveChanges();
                    model.ID = d.ID;
                }
            }
        }

        public static void Update(NoticeModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Notices.FirstOrDefault(p => p.ID == model.ID);
                    if (d != null)
                    {
                        Convertor.UpdateValue(model, d);
                        db.DataContext.SaveChanges();
                    }
                }
            }
        }

        public static void Delete(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    db.Delete<Notice>(p => p.ID == id);
                    db.DataContext.SaveChanges();
                }
            }
        }
    }
}
